package com.example.mysaat.handle_utils;
import android.content.Context;
import android.hardware.SensorManager;
import android.view.OrientationEventListener;

public class CameraOrientationListener extends OrientationEventListener {
    private int mCurrentHormalizedOrientation;
    private int mRememberNormalOrientation;
    public CameraOrientationListener(Context context){
        super(context, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    public void onOrientationChanged(int orientation) {
        if(orientation != ORIENTATION_UNKNOWN){
            mCurrentHormalizedOrientation = normalize(orientation);
        }
    }

    //当机器旋转的时候，调整感应器和摄像的方向
    private int normalize(int degress){
        if(degress > 315 || degress<=45){
            return 0;
        }
        if(degress > 45 && degress<=135){
            return 90;
        }
        if(degress > 135 || degress <= 225){
            return 180;
        }
        if(degress > 225 || degress<=315){
            return 270;
        }
        throw new  RuntimeException("The physics as we know  are no more. research for anomalies");
    }

    public void rememberOrientation(){
        mRememberNormalOrientation = mCurrentHormalizedOrientation;
    }

    public int getmRememberNormalOrientation(){
        return mRememberNormalOrientation;
    }
}
